package com.entor.springboot201912.service;

import com.entor.springboot201912.dao.StudentDaoI;
import com.entor.springboot201912.model.Msg;
import com.entor.springboot201912.model.Student;
import com.entor.springboot201912.model.Test;
import com.entor.springboot201912.util.RedisUtil;
import org.apache.jasper.tagplugins.jstl.core.ForEach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StudentService implements  StudentServiceI {

    @Autowired(required = false)
    private StudentDaoI sdi;
    @Autowired
    private RedisUtil<Student> rs;

    @Override
    public List<Student> find(Student s) {
        return sdi.find(s);
    }

    @Override
    public Student get(Student s) {
        List<Student> list = this.find(s);
        return list.size()>0?list.get(0):null;
    }

    @Override
    public Msg add(Student student) {
        int i = sdi.add(student);
        Msg m = new Msg("保存失败",false,null);
        if(i>0){
            m = new Msg("保存成功",true,null);
        }
        return m;
    }

    @Override
    public Msg edit(Student student) {
        int i = sdi.edit(student);
        Msg m = new Msg("保存失败",false,null);
        if(i>0){
            m = new Msg("保存成功",true,null);
        }
        return m;
    }

    @Override
    public Msg remove(int id) {
        int i = sdi.remove(id);
        Msg m = new Msg("删除失败",false,null);
        if(i>0){
            m = new Msg("删除成功",true,null);
        }
        return m;
    }

    @Override
    public Map callProcedure(int cid) {
        Map map = new HashMap();
        map.put("cid",cid);
        map.put("cname",null);
        map.put("con",null);
        sdi.callProcedure(map);
        return map;
    }

    @Override
    public Map callResultSet(int cid) {
        Map<String,Object> map = new HashMap();
        map.put("cid",cid);
        sdi.callResultSet(map);
        List<Test> rs = (List) map.get("resultSet");//通过key获取value
        try {
            for(Test t:rs) {
                System.out.println(t.getCname());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return map;
    }

    @Override
    public boolean setList(String key) {
        List<Student> list = this.find(new Student());
        return rs.setList(key,list);
    }
}
